package com.fingard.dsp.bank.directbank.bochk01;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IForeignCurrency;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.ReqForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.ReqQryForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.RetAtsForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.detail.RetAtsForeignCurrPayDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOCHK01ForeignCurrSinglePay extends BOCHK01Base implements IForeignCurrency {
    private static final String INSERTSEQ = "INSERT INTO REQSEQIDMAPPING(URID,BANKCODE,TRANSDATE,REQSEQID,BANKREQSEQID) values (?,?,?,?,?);";
    private static final String SELECTSEQ = "SELECT BANKREQSEQID FROM REQSEQIDMAPPING WHERE REQSEQID=? AND BANKCODE=?;";

    @Override
    public RetAtsForeignCurrSingle foreignCurrPay(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception {
        RetAtsForeignCurrSingle retAts = new RetAtsForeignCurrSingle(reqForeignCurrSingle.reqHeader);
        RetAtsForeignCurrPayDetail tmpRetDetail = null;
        try {
            if ("1".equals(reqForeignCurrSingle.singleDetail.sameBnk)) {
                tmpRetDetail = internalTrans(reqForeignCurrSingle);
            } else if ("1".equals(reqForeignCurrSingle.singleDetail.sameCity)) {
                tmpRetDetail = localBankTrans(reqForeignCurrSingle);
            } else {
                tmpRetDetail = teleTransfer(reqForeignCurrSingle);
            }
        } catch (Exception ex) {
            tmpRetDetail = new RetAtsForeignCurrPayDetail();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            ex.printStackTrace();
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    /**
     * 6.2.8	同行转账 (TransferREQ - InternalTrans)
     *
     * @param reqForeignCurrSingle
     * @return
     */
    private RetAtsForeignCurrPayDetail internalTrans(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception {
        RetAtsForeignCurrPayDetail tmpRetDetail = new RetAtsForeignCurrPayDetail();
        Connection conn= FGBiz.getLocalDbConn();
        try {
            //赋值通用参数
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            BOCHK01Header tmpHeader = new BOCHK01Header();
            tmpHeader.packageId = getQryTransID2() + "0";
            //记录ats支付流水号对应的银行支付流水号，供查询状态接口使用
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            JDBCHelper.update(conn, INSERTSEQ, new Object[]{uuid, reqForeignCurrSingle.reqHeader.bankCode, Format.Date8Format.format(new Date()), reqForeignCurrSingle.singleDetail.askNumber, tmpHeader.packageId});
            writeCommonHeader(xmlWriter, tmpHeader);
            xmlWriter.writeStartElement("TransferREQ");
            xmlWriter.writeElementString("DebitAcctNo","");
            xmlWriter.writeElementString("DebitCur","");
            xmlWriter.writeElementAttribute("Requests", "noOfRecord","1");//����^
            xmlWriter.writeStartElement("Record");
            XElement tmpTransXElement = new XElement("InternalTrans", "");
            tmpTransXElement.addChild(new XElement("DDebitAcctNo", reqForeignCurrSingle.singleDetail.payAct));//付款账号
            tmpTransXElement.addChild(new XElement("DDebitCur", convertCurToBank(reqForeignCurrSingle.singleDetail.payCur)));//付款币种
            tmpTransXElement.addChild(new XElement("DebitAmt", ""));//付款金额
            tmpTransXElement.addChild(new XElement("BeneAcctNo", reqForeignCurrSingle.singleDetail.recAct));//收款人账号
            tmpTransXElement.addChild(new XElement("BeneAcctName", reqForeignCurrSingle.singleDetail.recName));//收款人名称/地址
            tmpTransXElement.addChild(new XElement("PaymentCur", convertCurToBank(reqForeignCurrSingle.singleDetail.recCur)));//收款币种
            tmpTransXElement.addChild(new XElement("EquvAmt", reqForeignCurrSingle.singleDetail.payAmount));//汇款金额
            tmpTransXElement.addChild(new XElement("CustRef", reqForeignCurrSingle.singleDetail.askNumber));//客户参考号
            String exContract = "";
            if (!reqForeignCurrSingle.singleDetail.payCur.equals(reqForeignCurrSingle.singleDetail.recCur)) {
                if (StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.exContract)) {
                    exContract = reqForeignCurrSingle.singleDetail.exContract;
                } else {
                    //根据付方账号+付方币种+收方账号+收方币种取汇率合约号
                    exContract = getBankConfig().getMapAdvanceValuesExtra("exContractMapping", new String[]{"payAct", "payCur", "recAct", "recCur"}, "exContract", new String[]{reqForeignCurrSingle.singleDetail.payAct, reqForeignCurrSingle.singleDetail.payCur, reqForeignCurrSingle.singleDetail.recAct, reqForeignCurrSingle.singleDetail.recCur});
                }
            }
            tmpTransXElement.addChild(new XElement("ExContract", exContract));//汇率合约号
            tmpTransXElement.addChild(new XElement("ValueDate", ""));//生效日期
            nodeAnalysis(reqForeignCurrSingle.singleDetail.srcXElement, tmpTransXElement, getCharset());
            xmlWriter.writeXElement(tmpTransXElement);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            //处理响应报文
            tmpRetDetail=payResult(tmpStrRet,reqForeignCurrSingle.singleDetail.askNumber);
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
        }finally {
            conn.close();
        }
        return tmpRetDetail;
    }

    /**
     * 6.2.9	本地银行转账 (TransferREQ - LocalBankTrans)
     *
     * @param reqForeignCurrSingle
     * @return
     */

    private RetAtsForeignCurrPayDetail localBankTrans(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception {
        RetAtsForeignCurrPayDetail tmpRetDetail = new RetAtsForeignCurrPayDetail();
        Connection conn= FGBiz.getLocalDbConn();
        try {
            //赋值通用参数
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            BOCHK01Header tmpHeader = new BOCHK01Header();
            tmpHeader.packageId = getQryTransID2() + "1";
            //记录ats支付流水号对应的银行支付流水号，供查询状态接口使用
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            JDBCHelper.update(conn, INSERTSEQ, new Object[]{uuid, reqForeignCurrSingle.reqHeader.bankCode, Format.Date8Format.format(new Date()), reqForeignCurrSingle.singleDetail.askNumber, tmpHeader.packageId});
            writeCommonHeader(xmlWriter, tmpHeader);
            xmlWriter.writeStartElement("TransferREQ");
            xmlWriter.writeElementString("DebitAcctNo","");
            xmlWriter.writeElementString("DebitCur","");
            xmlWriter.writeElementAttribute("Requests","noOfRecord","1");
            XElement tmpRecordXElement = new XElement("Record", "");
            XElement tmpTransXElement = new XElement("LocalBankTrans", "");
            tmpTransXElement.addChild(new XElement("DDebitAcctNo", reqForeignCurrSingle.singleDetail.payAct));//付款账号
            tmpTransXElement.addChild(new XElement("DDebitCur", convertCurToBank(reqForeignCurrSingle.singleDetail.payCur)));//付款币种
            tmpTransXElement.addChild(new XElement("DebitAmt", ""));//付款金额
            tmpTransXElement.addChild(new XElement("BeneBankCode", reqForeignCurrSingle.singleDetail.recRTGSCode));//收款人所在行rtgs号
            String tmpRecAct = reqForeignCurrSingle.singleDetail.recAct;
            String tmpBeneNameAddr = "";
            if (StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.recAct)) {
                if (reqForeignCurrSingle.singleDetail.recAct.length() > 6) {
                    /*将客商银行账号前3位与其银行类别编码（RTGS码）进行比对：
                    1)两者相等，则截取掉客商账户前3位
                    2)如果不相等，则不截取*/
                    String tmpActH3 = reqForeignCurrSingle.singleDetail.recAct.substring(0, 3);
                    if (tmpActH3.equals(reqForeignCurrSingle.singleDetail.recRTGSCode)) {
                        tmpRecAct = tmpRecAct.substring(3);
                    }
                    //对前一步处理后的客商账号前三位进行截取，放入BeneBranchCode字段。
                    String tmpBeneBranchCode = tmpRecAct.substring(0, 3);
                    tmpTransXElement.addChild(new XElement("BeneBranchCode", tmpBeneBranchCode));//收款人賬戶分支行代號
                    tmpRecAct = tmpRecAct.substring(3);
                    //1、	此时受益人银行账户剩余长度<=28，直接写入支付报文的BeneAcctNo字段；
                    if (tmpRecAct.length() <= 28) {
                        tmpTransXElement.addChild(new XElement("BeneAcctNo", tmpRecAct));//收款人账号
                    } else {
                        //2、	受益人银行账户剩余长度>28位时，将前28位写入支付报文BeneAcctNo字段，并且将当前账号（AC NO bank code+branch code+截取剩余部分）写入到BeneNameAddr字段；
                        tmpTransXElement.addChild(new XElement("BeneAcctNo", tmpRecAct.substring(0, 28)));//收款人账号
                        tmpBeneNameAddr = "AC NO " + reqForeignCurrSingle.singleDetail.recRTGSCode + tmpBeneBranchCode + tmpRecAct;
                    }
                } else {//当客商账户<=6位,受益人账号处理逻辑
                    tmpTransXElement.addChild(new XElement("BeneBranchCode", ""));//收款人賬戶分支行代號
                    tmpTransXElement.addChild(new XElement("BeneAcctNo", tmpRecAct));//收款人账号
                    tmpBeneNameAddr = "AC NO " + reqForeignCurrSingle.singleDetail.recRTGSCode + tmpRecAct;
                }
            } else {
                tmpTransXElement.addChild(new XElement("BeneBranchCode", ""));//收款人賬戶分支行代號
                tmpTransXElement.addChild(new XElement("BeneAcctNo", ""));//收款人账号
            }
            XElement tmpBeneNameAddrXE = new XElement("BeneNameAddr", "");//收款人名称/地址
            ArrayList<String> listAddr = cutAddrString(reqForeignCurrSingle.singleDetail.recName, 4);
            ArrayList<String> listNameAddr = cutAddrString(tmpBeneNameAddr, 4);
            if (listAddr.size() + listNameAddr.size() > 4) {
                listAddr = cutAddrString(reqForeignCurrSingle.singleDetail.recName + " " + tmpBeneNameAddr, 4);
            } else if (listNameAddr.size() > 0) {
                listAddr.addAll(listNameAddr);
            }
            spiltToLine(tmpBeneNameAddrXE, listAddr);
            tmpTransXElement.addChild(tmpBeneNameAddrXE);
            tmpTransXElement.addChild(new XElement("PaymentCur", convertCurToBank(reqForeignCurrSingle.singleDetail.recCur)));//收款人币种
            tmpTransXElement.addChild(new XElement("EquvAmt", reqForeignCurrSingle.singleDetail.payAmount));//汇款金额

            XElement tmpChargeFromXE = new XElement("ChargeFrom", "");
            //手续费收款人承担
            if("BEN".equals(reqForeignCurrSingle.singleDetail.costMode)){
                tmpTransXElement.addChild(new XElement("ChargeFromBene", ""));
            }else {//手续费汇款人承担或共同承担
                XElement tmpChargeFromAcctXE = new XElement("ChargeFromAcct", "");//手续费账户
                //汇款人承担
                tmpChargeFromAcctXE.addChild(new XElement("ChargeAcctNo", StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.feeAct)
                        ?reqForeignCurrSingle.singleDetail.feeAct:reqForeignCurrSingle.singleDetail.payAct));//手续费账号
                tmpChargeFromAcctXE.addChild(new XElement("ChargeCur", StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.chargeCur)
                        ?reqForeignCurrSingle.singleDetail.chargeCur:convertCurToBank(reqForeignCurrSingle.singleDetail.payCur)));//手续费币种
                if("OUR".equals(reqForeignCurrSingle.singleDetail.costMode)){
                    tmpChargeFromAcctXE.addChild(new XElement("ChargeBorneBy","A"));//
                }else if("SHA".equals(reqForeignCurrSingle.singleDetail.costMode)){
                    tmpChargeFromAcctXE.addChild(new XElement("ChargeBorneBy","B"));//
                }
                tmpChargeFromXE.addChild(tmpChargeFromAcctXE);
                tmpTransXElement.addChild(tmpChargeFromXE);
            }
            XElement tmpPaymentDetailXE = new XElement("PaymentDetail", "");//附言
            spiltToLine(tmpPaymentDetailXE, reqForeignCurrSingle.singleDetail.purpose, 4);
            tmpTransXElement.addChild(tmpPaymentDetailXE);
            tmpTransXElement.addChild(new XElement("CustRef", reqForeignCurrSingle.singleDetail.askNumber));//客��淇继�
            String exContract = "";
            if (!reqForeignCurrSingle.singleDetail.payCur.equals(reqForeignCurrSingle.singleDetail.recCur)) {
                if (StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.exContract)) {
                    exContract = reqForeignCurrSingle.singleDetail.exContract;
                } else {
                    //根据付方账号+付方币种+收方账号+收方币种取汇率合约号
                    exContract = getBankConfig().getMapAdvanceValuesExtra("exContractMapping", new String[]{"payAct", "payCur", "recAct", "recCur"}, "exContract", new String[]{reqForeignCurrSingle.singleDetail.payAct, reqForeignCurrSingle.singleDetail.payCur, reqForeignCurrSingle.singleDetail.recAct, reqForeignCurrSingle.singleDetail.recCur});
                }
            }
            tmpTransXElement.addChild(new XElement("ExContract", exContract));//汇率合约号
            tmpTransXElement.addChild(new XElement("ValueDate", ""));//生效日期
            nodeAnalysis(reqForeignCurrSingle.singleDetail.srcXElement, tmpTransXElement, getCharset());
            tmpRecordXElement.addChild(tmpTransXElement);
            xmlWriter.writeXElement(tmpRecordXElement);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            //处理响应报文
            tmpRetDetail=payResult(tmpStrRet,reqForeignCurrSingle.singleDetail.askNumber);
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
        }finally {
            conn.close();
        }
        return tmpRetDetail;
    }

    /**
     * 6.2.10  电汇 (TransferREQ - TeleTransfer)
     *
     * @param reqForeignCurrSingle
     * @return
     */
    private RetAtsForeignCurrPayDetail teleTransfer(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception {
        RetAtsForeignCurrPayDetail tmpRetDetail = new RetAtsForeignCurrPayDetail();
        Connection conn= FGBiz.getLocalDbConn();
        try {
            //赋值通用参数
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            BOCHK01Header tmpHeader = new BOCHK01Header();
            tmpHeader.packageId = getQryTransID2() + "2";
            //记录ats支付流水号对应的银行支付流水号，供查询状态接口使用
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            JDBCHelper.update(conn, INSERTSEQ, new Object[]{uuid, reqForeignCurrSingle.reqHeader.bankCode, Format.Date8Format.format(new Date()), reqForeignCurrSingle.singleDetail.askNumber, tmpHeader.packageId});
            writeCommonHeader(xmlWriter, tmpHeader);
            xmlWriter.writeStartElement("TransferREQ");
            xmlWriter.writeElementString("DebitAcctNo","");
            xmlWriter.writeElementString("DebitCur","");
            xmlWriter.writeElementAttribute("Requests","noOfRecord","1");
            XElement tmpRecordXElement = new XElement("Record", "");
            XElement tmpTransXElement = new XElement("TeleTransfer", "");
            tmpTransXElement.addChild(new XElement("DDebitAcctNo", reqForeignCurrSingle.singleDetail.payAct));//付款账号
            tmpTransXElement.addChild(new XElement("DDebitCur", convertCurToBank(reqForeignCurrSingle.singleDetail.payCur)));//付款币种
            tmpTransXElement.addChild(new XElement("RemitCur", convertCurToBank(reqForeignCurrSingle.singleDetail.recCur)));//收款币种
            tmpTransXElement.addChild(new XElement("RemitAmt", reqForeignCurrSingle.singleDetail.payAmount));//汇款金额
            tmpTransXElement.addChild(new XElement("EquvAmt", ""));
            XElement tmpChargeFromXE = new XElement("ChargeFrom", "");
            //手续费收款人承担
            if("BEN".equals(reqForeignCurrSingle.singleDetail.costMode)){
                tmpTransXElement.addChild(new XElement("ChargeFromBene", ""));
            }else {//手续费汇款人承担或共同承担
                XElement tmpChargeFromAcctXE = new XElement("ChargeFromAcct", "");//手续费账户
                //汇款人承担
                tmpChargeFromAcctXE.addChild(new XElement("ChargeAcctNo", StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.feeAct)
                        ?reqForeignCurrSingle.singleDetail.feeAct:reqForeignCurrSingle.singleDetail.payAct));//手续费账号
                tmpChargeFromAcctXE.addChild(new XElement("ChargeCur", StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.chargeCur)
                        ?reqForeignCurrSingle.singleDetail.chargeCur:convertCurToBank(reqForeignCurrSingle.singleDetail.payCur)));//手续费币种
                if("OUR".equals(reqForeignCurrSingle.singleDetail.costMode)){
                    tmpChargeFromAcctXE.addChild(new XElement("ChargeBorneBy","A"));//
                }else if("SHA".equals(reqForeignCurrSingle.singleDetail.costMode)){
                    tmpChargeFromAcctXE.addChild(new XElement("ChargeBorneBy","B"));//
                }
                tmpChargeFromXE.addChild(tmpChargeFromAcctXE);
                tmpTransXElement.addChild(tmpChargeFromXE);
            }
            //收款银行信息
            tmpTransXElement.addChild(new XElement("BeneBankerSwiftCode", reqForeignCurrSingle.singleDetail.recSWIFTCODE));//收款银行swiftcode
            String recBankCountry="";
            if(StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.recCountryCode)){
                recBankCountry=reqForeignCurrSingle.singleDetail.recCountryCode;
            }else {
                recBankCountry=reqForeignCurrSingle.singleDetail.recCountry;
            }
            tmpTransXElement.addChild(new XElement("BeneBankerCountry", convertCountryToBank(recBankCountry)));//收款银行国家/地区
            XElement tmpBeneBankerNameXE = new XElement("BeneBankerName", "");
            String bankName="";
            if(StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.recCNAPSName)){
                bankName=reqForeignCurrSingle.singleDetail.recCNAPSName;
            }else if(StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.recBnkName)){
                bankName=reqForeignCurrSingle.singleDetail.recBnkName;
            }
            String bankAddr="";
            if(StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.recBnkAddr)){
                bankAddr=reqForeignCurrSingle.singleDetail.recBnkAddr;
            }else if(StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.recAddr)){
                bankAddr=reqForeignCurrSingle.singleDetail.recAddr;
            }
            if(StringHelper.isNullOrEmpty(reqForeignCurrSingle.singleDetail.recSWIFTCODE)){
                String beneBankerName = bankName + " " + bankAddr;
                spiltToLine(tmpBeneBankerNameXE, beneBankerName, 3);
            }else {
                tmpBeneBankerNameXE.addChild(new XElement("Line", ""));//按银行要求Line至少存在一个
            }
            tmpTransXElement.addChild(tmpBeneBankerNameXE);
            //清算要求参数
            tmpTransXElement.addChild(new XElement("BeneBankerCountryCodeWords", ""));
            tmpTransXElement.addChild(new XElement("CnapsNum", ""));
            tmpTransXElement.addChild(new XElement("RMBRemitTxType", getBankConfig().getMapValueByContainsKey("RMBRemitTxTypeMapping", reqForeignCurrSingle.singleDetail.purpose)));//人民幣匯款交易種類
            tmpTransXElement.addChild(new XElement("RemitBeneAcctNo", reqForeignCurrSingle.singleDetail.recAct));//匯款收款人賬戶號碼
            XElement tmpRemitBeneNameXE = new XElement("RemitBeneName", "");//收款人名称/地址
            if(StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.recName)){
                tmpRemitBeneNameXE.addChild(new XElement("Line",reqForeignCurrSingle.singleDetail.recName));
            }
            if(StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.recAddr)) {
                tmpRemitBeneNameXE.addChild(new XElement("Line",reqForeignCurrSingle.singleDetail.recAddr));
            }
            tmpTransXElement.addChild(tmpRemitBeneNameXE);
            if (StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.recAgentBnkAct)) {
                tmpTransXElement.addChild(new XElement("BeneBankerAcct", reqForeignCurrSingle.singleDetail.recAgentBnkAct));//代理行�~�
                tmpTransXElement.addChild(new XElement("CorreBeneBankerCountry", convertCountryToBank(reqForeignCurrSingle.singleDetail.recAgentBnkCtryCode)));//代理行��家/地�^
                tmpTransXElement.addChild(new XElement("CorreBeneBankerSwiftCode", reqForeignCurrSingle.singleDetail.recAgentBnkSWIFT));//代理行SWIFT BIC CODE
                XElement tmpCorreBeneBankerXE = new XElement("CorreBeneBanker", "");//代理行名称及地址
                if (StringHelper.isNullOrEmpty(reqForeignCurrSingle.singleDetail.recAgentBnkSWIFT)) {
                    String correBeneBanker = reqForeignCurrSingle.singleDetail.recAgentBnkName + " " + reqForeignCurrSingle.singleDetail.recAgentBnkAddr;
                    spiltToLine(tmpCorreBeneBankerXE, correBeneBanker, 3);
                } else {
                    tmpCorreBeneBankerXE.addChild(new XElement("Line", ""));//按银行要求Line至少存在一个
                }
                tmpTransXElement.addChild(tmpCorreBeneBankerXE);

            } else {
                tmpTransXElement.addChild(new XElement("BeneBankerAcct", ""));//代理行�~�
                tmpTransXElement.addChild(new XElement("CorreBeneBankerCountry", ""));//代理行��家/地�^
                tmpTransXElement.addChild(new XElement("CorreBeneBankerSwiftCode", ""));//代理行SWIFT BIC CODE

                XElement tmpCorreBeneBankerXE = new XElement("CorreBeneBanker", "");//代理行名�Q及地址
                tmpCorreBeneBankerXE.addChild(new XElement("Line", ""));//按银行要求Line至少存在一个
                tmpTransXElement.addChild(tmpCorreBeneBankerXE);
            }

            XElement tmpPaymentDetailXE = new XElement("PaymentDetail", "");//附言
            spiltToLine(tmpPaymentDetailXE, reqForeignCurrSingle.singleDetail.purpose, 4);
            tmpTransXElement.addChild(tmpPaymentDetailXE);

            tmpTransXElement.addChild(new XElement("MessageToBank", ""));//���y行指示
            tmpTransXElement.addChild(new XElement("CustRef", reqForeignCurrSingle.singleDetail.askNumber));//客��淇继�
            String exContract = "";
            if (!reqForeignCurrSingle.singleDetail.payCur.equals(reqForeignCurrSingle.singleDetail.recCur)) {
                if (StringHelper.hasAnyChar(reqForeignCurrSingle.singleDetail.exContract)) {
                    exContract = reqForeignCurrSingle.singleDetail.exContract;
                } else {
                    //根据付方账号+付方币种+收方账号+收方币种取汇率合约号
                    exContract = getBankConfig().getMapAdvanceValuesExtra("exContractMapping", new String[]{"payAct", "payCur", "recAct", "recCur"}, "exContract", new String[]{reqForeignCurrSingle.singleDetail.payAct, reqForeignCurrSingle.singleDetail.payCur, reqForeignCurrSingle.singleDetail.recAct, reqForeignCurrSingle.singleDetail.recCur});
                }
            }
            tmpTransXElement.addChild(new XElement("ExContract", exContract));//�R率合�s
            tmpTransXElement.addChild(new XElement("ValueDate", ""));//生效日期
            nodeAnalysis(reqForeignCurrSingle.singleDetail.srcXElement, tmpTransXElement, getCharset());
            tmpRecordXElement.addChild(tmpTransXElement);
            xmlWriter.writeXElement(tmpRecordXElement);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            //处理响应报文
            tmpRetDetail=payResult(tmpStrRet,reqForeignCurrSingle.singleDetail.askNumber);
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
        }finally {
            conn.close();
        }
        return tmpRetDetail;
    }

    /**
     * 付款响应报文处理
     * @param tmpStrRet
     * @param askNumber
     * @return
     * @throws Exception
     */
    private RetAtsForeignCurrPayDetail payResult(String[] tmpStrRet,String askNumber) throws Exception{
        RetAtsForeignCurrPayDetail tmpRetDetail = new RetAtsForeignCurrPayDetail();
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String txStatus = tmpBkReader.getFirstText("/BOCHKB2E/Head/TxStatus");
                if ("S".equals(txStatus) || "R".equals(txStatus) || "F".equals(txStatus)) {
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.respInfo = tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorDesc");
                    tmpRetDetail.reqSeqID = askNumber;
                    if ("F".equals(txStatus)) {
                        if (tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode") != null) {
                            tmpRetDetail.transState = txStatus + "+" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode");
                            tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode");
                        }
                    } else if ("R".equals(txStatus)) {
                        if (tmpBkReader.getFirstText("/BOCHKB2E/TX/TransferRLY/Requests/Record/TeleTransfer/RecordErrorCode") != null) {
                            tmpRetDetail.transState = txStatus + "+" + tmpBkReader.getFirstText("/BOCHKB2E/TX/TransferRLY/Requests/Record/TeleTransfer/RecordStatus");
                            tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/BOCHKB2E/TX/TransferRLY/Requests/Record/TeleTransfer/RecordErrorCode");
                            tmpRetDetail.respInfo = tmpBkReader.getFirstText("/BOCHKB2E/TX/TransferRLY/Requests/Record/TeleTransfer/RecordErrorDesc");
                        } else {
                            tmpRetDetail.transState = txStatus + "+" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode");
                            tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode");
                        }
                    } else {
                        if (tmpBkReader.getFirstText("/BOCHKB2E/TX/TransferRLY/Requests/Record/TeleTransfer/RecordErrorDesc") != null) {
                            tmpRetDetail.respInfo = tmpBkReader.getFirstText("/BOCHKB2E/TX/TransferRLY/Requests/Record/TeleTransfer/RecordErrorDesc");
                        }
                        tmpRetDetail.transState = txStatus + "+" + tmpBkReader.getFirstText("/BOCHKB2E/TX/TransferRLY/Requests/Record/TeleTransfer/RecordStatus");
                        if (tmpBkReader.getFirstText("/BOCHKB2E/TX/TransferRLY/Requests/Record/TeleTransfer/RecordErrorCode") != null) {
                            tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/BOCHKB2E/TX/TransferRLY/Requests/Record/TeleTransfer/RecordErrorCode");
                        } else {
                            tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/BOCHKB2E/TX/TransferRLY/Requests/Record/TeleTransfer/RecordStatus");
                            tmpRetDetail.respInfo = "提交成功";
                        }
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = "错误代码：" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode") + "，错误信息：" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorDesc");
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpSbMsg.toString();
            }
        } else {
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = tmpStrRet[1];
        }
        return tmpRetDetail;
    }

    @Override
    public RetAtsForeignCurrSingle qryForeignCurrPay(ReqQryForeignCurrSingle reqQryForeignCurrSingle) throws Exception {
        RetAtsForeignCurrSingle retAts = new RetAtsForeignCurrSingle(reqQryForeignCurrSingle.reqHeader);
        RetAtsForeignCurrPayDetail tmpRetDetail = new RetAtsForeignCurrPayDetail();
        Connection conn=FGBiz.getLocalDbConn();
        try {
            //String oriSeqID = DirectHelper.getReqSeqIDMapping(reqQryForeignCurrSingle.askNumber);//某位表示具体支付方式 0：�⑴c行�D�~  1：本地�y行�D�~ 2：��R
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            BOCHK01Header tmpHeader = new BOCHK01Header();
            tmpHeader.packageId = getQryTransID();
            writeCommonHeader(xmlWriter, tmpHeader);
            XElement tmpXElement = new XElement("TransferEnquiryREQ", "");
            Map<String, Object> selMap=JDBCHelper.execQuerySingle(conn,SELECTSEQ,new Object[]{reqQryForeignCurrSingle.askNumber,reqQryForeignCurrSingle.reqHeader.bankCode});
            if(selMap.size()>0){
                tmpXElement.addChild(new XElement("EnqPackageId", selMap.get("BANKREQSEQID").toString()));//包序列 ID
            }else {
                tmpRetDetail.respCode ="-1";
                tmpRetDetail.respInfo = "财务系统交易流水号关联映射异常，请检查";
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            tmpXElement.addChild(new XElement("TxDate", Format.Date10Format2.format(reqQryForeignCurrSingle.getBgnDateTime())));//交易日期
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String txStatus = tmpBkReader.getFirstText("/BOCHKB2E/Head/TxStatus");
                    if ("S".equals(txStatus) || "R".equals(txStatus)) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorDesc");
                        tmpRetDetail.reqSeqID = reqQryForeignCurrSingle.askNumber;
                        if ("R".equals(txStatus) && "E21007".equals(tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode"))) {
                            tmpRetDetail.transState = txStatus + "+" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode");
                            tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode");
                        } else {
                            String readNodePath="/BOCHKB2E/Tx/TransferEnquiryRLY/Requests/Record/";
                            if(tmpStrRet[1].contains("InternalTrans")){
                                readNodePath+="InternalTrans";
                            }else if(tmpStrRet[1].contains("LocalBankTrans")){
                                readNodePath+="LocalBankTrans";
                            }else if(tmpStrRet[1].contains("TeleTransfer")){
                                readNodePath+="TeleTransfer";
                            }
                            /*if (StringHelper.hasAnyChar(tmpBkReader.getFirstText(readNodePath + "/CustRef"))) {
                                tmpRetDetail.reqSeqID = tmpBkReader.getFirstText(readNodePath + "/CustRef");
                            }else {
                                tmpRetDetail.reqSeqID=reqQryForeignCurrSingle.askNumber;
                            }*/
                            tmpRetDetail.transState = tmpBkReader.getFirstText(readNodePath + "/TxStatus");
                            tmpRetDetail.payInfoCode =tmpRetDetail.transState;
                            if (tmpBkReader.getFirstText(readNodePath + "/RejectReason") != null) {
                                tmpRetDetail.respInfo = tmpBkReader.getFirstText(readNodePath + "/RejectReason");
                                tmpRetDetail.payInfoCode = tmpBkReader.getFirstText(readNodePath + "/RejectReason");
                            }
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = "错误代码：" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode") + "，错误信息：" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorDesc");
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            tmpRetDetail = new RetAtsForeignCurrPayDetail();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            ex.printStackTrace();
        }finally {
            conn.close();
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
}
